<?php
require_once("../inc/functions.php");

session_start();
function DoLogin($email, $password) 
{
        if(!empty($email) && !empty($password) && checkPassword($email, $password) && checkEmail($email))
        {
                $_SESSION['User'] = array('name'=>$email);
                return true;
        }else{
                return array('Error'=> 'Wrong E-mail or Password ');
        }
}
function IsLoggedIn() 
{
        return (GetCurrentUser() != null);
}
function GetCurrentUser()
{
        return isset($_SESSION['User']) ? $_SESSION['User'] : null;
}
function RequireLogin()
{
        if(!IsLoggedIn())
        {
                header("Location: ../Login/login.php?returnUrl=$_SERVER[PHP_SELF]");
                die();
        }
}

function checkPassword($email, $password)
{
	$conn = getConnection();
	$sql = "Select keyword from User u join Customer_email ce on u.user_ID = ce.user_ID Where email='$email'";
	$results = $conn->query($sql);
	$results = $results->fetch_assoc();
	
	if($results['keyword'] == $password)
		return true;
	else
		return false;
}

function checkEmail($email)
{
	$conn = getConnection();
	$sql = "Select email from Customer_email where email='$email'";
	$results = $conn->query($sql);
	$results = $results->fetch_assoc();
	
	if($results['email'] == $email)
		return true;
	else
		return false;
}
	
